﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThemChucVu : Form
    {
        public ThemChucVu()
        {
            InitializeComponent();
        }

        private void xoaThongTin()
        {
            MaSo_CV.Text = "";
            Ten_CV.Text = "";
            loadCombobox();
        }

        private void loadCombobox()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PhongBan";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            conn.Close();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PhongBan");
            PhongBanCombobox.DisplayMember = "Ten_PB";
            PhongBanCombobox.ValueMember = "MaSo_PB";
            PhongBanCombobox.DataSource = ds.Tables["PhongBan"].DefaultView;
        }

        private void themCV(string MaCV, string TenCV, string MaPB)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu where MaSo_CV = @MaSo_CV and MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_CV",MaCV);
            cmd.Parameters.AddWithValue("@MaSo_PB", MaPB);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                thongBaoLabel.Text = "Mã chức vụ đã tồn tại";
                reader.Close();
            }
            else
            {
                reader.Close();
                cmd.CommandText = "insert into ChucVu(MaSo_CV,Ten_CV,MaSo_PB) values(@MaSo_CV,@Ten_CV,@MaSo_PB)";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@MaSo_CV", MaCV);
                cmd.Parameters.AddWithValue("@Ten_CV", TenCV);
                cmd.Parameters.AddWithValue("@MaSo_PB", MaPB);
                cmd.ExecuteNonQuery();
                thongBaoLabel.Text = "Thêm thành công !";
            }
            conn.Close();
        }

        private void Them_Click(object sender, EventArgs e)
        {
            themCV(MaSo_CV.Text, Ten_CV.Text, PhongBanCombobox.SelectedValue.ToString());
            xoaThongTin();
        }

        private void quayLai_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ThemChucVu_Load(object sender, EventArgs e)
        {
            loadCombobox();
        }

        private void MaSo_CV_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(MaSo_CV.Text, "^[A-Za-z]{1,6}$"))
                MaCVError.Text = "";
            else
                MaCVError.Text = "*";

            if (MaSo_CV.Text.Length == 0)
                MaCVError.Text = "*";
        }
    }
}
